package net.bezrabotnih.cabinet.client.ui;

import net.bezrabotnih.client.managed.request.JobProxy;
import net.bezrabotnih.client.managed.request.UserAccountProxy;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Hyperlink;

public class ApplicantBlockView extends Composite {

	public interface Presenter{
		
		void confirmClicked(String applicantId,String jobId);
		
		void cancelClicked(String applicantId,String jobId);
		
        void showApplicant(UserAccountProxy userAccountProxy);
		
		void showJob(JobProxy jobProxy);
	}
	
	
	
	
	private static ApplicantBlockViewUiBinder uiBinder = GWT
			.create(ApplicantBlockViewUiBinder.class);
	@UiField Image applicantThumbnail;
	@UiField Anchor applicantLink;
	@UiField Button confirmButton;
	@UiField Anchor jobLink;
	
	private boolean confirmed;
	private String applicantId;
	private String jobId;
	
	private Presenter listener;

	interface ApplicantBlockViewUiBinder extends
			UiBinder<Widget, ApplicantBlockView> {
	}

	public ApplicantBlockView() {
		initWidget(uiBinder.createAndBindUi(this));
	}
	
	public ApplicantBlockView(Presenter listener,final UserAccountProxy applicant,final JobProxy job,boolean confirmed) {
		initWidget(uiBinder.createAndBindUi(this));
		this.listener=listener;
		this.confirmed=confirmed;
		this.applicantId=applicant.getId();
		this.jobId=job.getId();
		applicantLink.setText(applicant.getName());
		this.jobLink.setText(job.getName());
		confirmButton.setText(confirmed?"Отказаться":"Утвердить");
		confirmButton.addClickHandler(new ClickHandler() {			
			@Override
			public void onClick(ClickEvent event) {
				ApplicantBlockView.this.confirmButton.setEnabled(false);
				if (!ApplicantBlockView.this.confirmed){
					ApplicantBlockView.this.listener.confirmClicked(ApplicantBlockView.this.applicantId, ApplicantBlockView.this.jobId);
				}else{
					ApplicantBlockView.this.listener.cancelClicked(ApplicantBlockView.this.applicantId, ApplicantBlockView.this.jobId);
				}
				
			}
		});
		applicantLink.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				ApplicantBlockView.this.listener.showApplicant(applicant);
				
			}
		});
		
		jobLink.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				ApplicantBlockView.this.listener.showJob(job);
				
			}
		});

	}
	
	public ApplicantBlockView(Presenter listener,String thumbnailUrl,String applicantId,String applicantName,String jobId,String jobName,boolean confirmed) {
		initWidget(uiBinder.createAndBindUi(this));
		this.listener=listener;
		this.confirmed=confirmed;
		this.applicantId=applicantId;
		this.jobId=jobId;
		applicantLink.setText(applicantName);
		this.jobLink.setText(jobName);
		confirmButton.setText(confirmed?"Отказаться":"Утвердить");
		confirmButton.addClickHandler(new ClickHandler() {			
			@Override
			public void onClick(ClickEvent event) {
				ApplicantBlockView.this.confirmButton.setEnabled(false);
				if (!ApplicantBlockView.this.confirmed){
					ApplicantBlockView.this.listener.confirmClicked(ApplicantBlockView.this.applicantId, ApplicantBlockView.this.jobId);
				}else{
					ApplicantBlockView.this.listener.cancelClicked(ApplicantBlockView.this.applicantId, ApplicantBlockView.this.jobId);
				}
				
			}
		});

	}
	
	public void confirmedSuccessView(){
		confirmButton.setEnabled(true);
		confirmed=true;
	}
	
	public void cancelSuccessView(){
		confirmButton.setEnabled(true);
		confirmed=false;
	}
	
    public void confirmedFailureView(){
    	confirmButton.setEnabled(true);
    	Window.alert("Ошибка сервера. Не удалось утвердить претендента.");
	}
	
	public void cancelFailureView(){
		confirmButton.setEnabled(true);
		Window.alert("Ошибка сервера. Не удалось оказаться от назначения.");
	}
	
	
	

}
